package com.ycrlkj.android.modulescore.main.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.lxj.xpopup.impl.LoadingPopupView;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.ycrlkj.android.modulescore.main.BR;
import com.ycrlkj.android.modulescore.main.R;
import com.ycrlkj.android.modulescore.main.apiservice.FreightOrderApiService;
import com.ycrlkj.android.modulescore.main.apiservice.OrderStateApiService;
import com.ycrlkj.android.modulescore.main.databinding.ActivityFreightOrderDetailBinding;
import com.ycrlkj.android.modulescore.main.entity.FreightOrder;
import com.ycrlkj.android.modulescore.main.entity.OrderState;
import com.ycrlkj.android.modulescore.main.viewmodel.FreightOrderViewModel;
import com.ycrlkj.android.modulespublic.common.base.DataBindingRecyclerViewAdapter;
import com.ycrlkj.android.modulespublic.common.constant.RoutePath;
import com.ycrlkj.android.modulespublic.common.net.exception.ApiException;
import com.ycrlkj.android.modulespublic.common.net.exception.ApiExceptionHandler;
import com.ycrlkj.android.modulespublic.common.net.exception.ErrorConsumer;
import com.ycrlkj.android.modulespublic.common.net.transformer.ResponseTransformer;
import com.ycrlkj.android.modulespublic.common.utils.xpopup.XPopupUtil;
import com.ycrlkj.android.modulespublic.common.widget.titlebar.MTitleBar;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;
import io.reactivex.functions.Consumer;

@AndroidEntryPoint
@Route(path = RoutePath.PAGE_FREIGHT_ORDER_DETAIL)
public class FreightOrderDetailActivity extends AppCompatActivity {

    private ActivityFreightOrderDetailBinding binding;

    @Inject
    FreightOrderApiService freightOrderApiService;

    @Inject
    OrderStateApiService orderStateApiService;

    @Autowired
    String id;

    private MTitleBar titleBar;

    private FreightOrderViewModel viewModel;

    private RecyclerView recyclerView;

    private DataBindingRecyclerViewAdapter<OrderState> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ARouter.getInstance().inject(this);

        if (id == null && id.length() != 19) {
            XPopupUtil.showAlert(this, "未找到订单信息, 关闭页面", new OnConfirmListener() {
                @Override
                public void onConfirm() {
                    FreightOrderDetailActivity.this.finish();
                }
            });
        }

        binding = ActivityFreightOrderDetailBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        viewModel = new ViewModelProvider(this).get(FreightOrderViewModel.class);

        titleBar = binding.mTitleBar;

        initTitleBar();
        initAdapter();
        initBtn();
        get(id);
        getAllOrderState(id);
    }

    public void initAdapter() {
        recyclerView = binding.recyclerView;
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new DataBindingRecyclerViewAdapter<>(this, R.layout.item_order_state, BR.orderState);
        recyclerView.setAdapter(adapter);
    }

    public void initBtn() {

    }

    @SuppressLint("CheckResult")
    public void getAllOrderState(String id) {
        orderStateApiService.getAllByOrderId(id)
                .compose(ResponseTransformer.obtain())
                .subscribe(list -> {
                    adapter.add(list);
                }, new ErrorConsumer() {
                    @Override
                    protected void error(ApiException e) {
                        ApiExceptionHandler.doWith(FreightOrderDetailActivity.this, e);
                    }
                });

    }

    @SuppressLint("CheckResult")
    public void get(String id) {
        LoadingPopupView loadingPopupView = XPopupUtil.showLoading(this);
        XPopupUtil.hideLoadingDelay(loadingPopupView);
        freightOrderApiService.getById(id)
                .compose(ResponseTransformer.obtain())
                .subscribe(new Consumer<FreightOrder>() {
                    @Override
                    public void accept(FreightOrder freightOrder) throws Exception {
                        XPopupUtil.hideLoading(loadingPopupView);
                        viewModel.getFreightOrder().setValue(freightOrder);
                        binding.setViewModel(viewModel);
                    }
                }, new ErrorConsumer() {
                    @Override
                    protected void error(ApiException e) {
                        XPopupUtil.hideLoading(loadingPopupView);
                        ApiExceptionHandler.doWith(FreightOrderDetailActivity.this, e);
                    }
                });

    }

    public void initTitleBar() {

        titleBar.setBtnEvents(new MTitleBar.BtnEvents() {
            @Override
            public void setBtnLeftOnClickListener() {
                FreightOrderDetailActivity.this.finish();
            }

            @Override
            public void setBtnRightOnClickListener() {

            }
        });
    }


}